package com.yida.gwj.vote.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.api.domain.dto.GwjGVoteResultInnerDTO;
import com.yida.gwj.api.domain.request.GwjVoteResultInnerRequest;
import com.yida.gwj.common.utils.CheckDiffList;
import com.yida.gwj.vote.domain.GwjVoteHouse;
import com.yida.gwj.vote.domain.GwjVoteProject;
import com.yida.gwj.vote.domain.GwjVoteResultDetail;
import com.yida.gwj.vote.domain.dto.GwjEVoteResultDetailListDTO;
import com.yida.gwj.vote.domain.dto.GwjGVoteResultDetailListDTO;
import com.yida.gwj.vote.domain.request.GwjVoteResultDetailQueryRequest;
import com.yida.gwj.vote.enums.GwjVoteStatusEnum;
import com.yida.gwj.vote.mapper.GwjVoteResultDetailMapper;
import com.yida.gwj.vote.service.IGwjVoteProjectService;
import com.yida.gwj.vote.service.IGwjVoteResultDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 投票结果详情Service业务层处理
 *
 * @author lqq
 * @date 2023-03-22
 */
@Service
public class GwjVoteResultDetailServiceImpl implements IGwjVoteResultDetailService {

    @Resource
    private GwjVoteResultDetailMapper voteResultDetailMapper;

    @Resource
    private IGwjVoteProjectService voteProjectService;

    /**
     * 查询投票结果详情
     *
     * @param detailId 投票结果详情主键
     * @return 投票结果详情
     */
    @Override
    public GwjVoteResultDetail selectVoteResultDetailByDetailId(Long detailId) {
        return voteResultDetailMapper.selectVoteResultDetailByDetailId(detailId);
    }

    /**
     * 查询投票结果详情列表
     *
     * @param voteResultDetail 投票结果详情
     * @return 投票结果详情
     */
    @Override
    public List<GwjVoteResultDetail> selectVoteResultDetailList(GwjVoteResultDetail voteResultDetail) {
        return voteResultDetailMapper.selectVoteResultDetailList(voteResultDetail);
    }

    /**
     * 新增投票结果详情
     *
     * @param voteResultDetail 投票结果详情
     * @return 结果
     */
    @Override
    public int insertVoteResultDetail(GwjVoteResultDetail voteResultDetail) {
        voteResultDetail.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        voteResultDetail.setCreateTime(DateUtils.getNowDate());
        return voteResultDetailMapper.insertVoteResultDetail(voteResultDetail);
    }

    /**
     * 修改投票结果详情
     *
     * @param voteResultDetail 投票结果详情
     * @return 结果
     */
    @Override
    public int updateVoteResultDetail(GwjVoteResultDetail voteResultDetail) {
        voteResultDetail.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        voteResultDetail.setUpdateTime(DateUtils.getNowDate());
        return voteResultDetailMapper.updateVoteResultDetail(voteResultDetail);
    }

    /**
     * 批量删除投票结果详情
     *
     * @param detailIds 需要删除的投票结果详情主键
     * @return 结果
     */
    @Override
    public int deleteVoteResultDetailByDetailIds(Long[] detailIds) {
        return voteResultDetailMapper.deleteVoteResultDetailByDetailIds(detailIds);
    }

    /**
     * 删除投票结果详情信息
     *
     * @param detailId 投票结果详情主键
     * @return 结果
     */
    @Override
    public int deleteVoteResultDetailByDetailId(Long detailId) {
        return voteResultDetailMapper.deleteVoteResultDetailByDetailId(detailId);
    }

    /**
     * 新增投票结果详情
     *
     * @param voteResultDetails 投票结果详情
     * @return 结果
     */
    @Override
    public int insertBatchVoteResultDetail(List<GwjVoteResultDetail> voteResultDetails) {
        return voteResultDetailMapper.insertBatchVoteResultDetail(voteResultDetails);
    }

    /**
     * 删除投票结果详情信息
     *
     * @param resultId 投票结果详情主键
     * @return 结果
     */
    @Override
    public int deleteVoteResultDetailByResultId(Long resultId) {
        return voteResultDetailMapper.deleteVoteResultDetailByResultId(resultId);
    }

    /**
     * 查询投票结果详情列表
     *
     * @param request 请求参数
     * @return 投票结果详情集合
     */
    @Override
    public List<GwjEVoteResultDetailListDTO> selectEVoteResultDetailDTOList(GwjVoteResultDetailQueryRequest request) {
        return voteResultDetailMapper.selectEVoteResultDetailDTOList(request);
    }

    /**
     * 查询投票结果详情列表
     *
     * @param request 请求参数
     * @return 投票结果详情集合
     */
    @Override
    public List<GwjGVoteResultDetailListDTO> selectGVoteResultDetailDTOList(GwjVoteResultDetailQueryRequest request) {
        return voteResultDetailMapper.selectGVoteResultDetailDTOList(request);
    }

    /**
     * 根据投票id统计投票人数
     *
     * @param voteId 投票ID
     * @return 结果
     */
    @Override
    public int countVoteOwnerByVoteId(Long voteId) {
        return voteResultDetailMapper.countVoteOwnerByVoteId(voteId);
    }

    /**
     * 根据投票id统计投票同意人数
     *
     * @param voteId 投票ID
     * @return 结果
     */
    @Override
    public int countAgreeVoteOwnerByVoteId(Long voteId) {
        return voteResultDetailMapper.countAgreeVoteOwnerByVoteId(voteId);
    }

    /**
     * 查询投票结果详情的房屋信息
     *
     * @param voteId 投票ID
     * @return 投票结果详情集合
     */
    @Override
    public List<GwjVoteHouse> selectVoteResultHouseListByVoteId(Long voteId) {
        return voteResultDetailMapper.selectVoteResultHouseListByVoteId(voteId);
    }

    /**
     * 查询投票结果详情的房屋信息
     *
     * @param voteResultDetail 投票详情
     * @return 投票结果详情集合
     */
    @Override
    public List<GwjVoteHouse> selectVoteResultHouseList(GwjVoteResultDetail voteResultDetail) {
        return voteResultDetailMapper.selectVoteResultHouseList(voteResultDetail);
    }

    /**
     * 根据投票选项统计投票结果详情
     *
     * @param optionId 投票选项ID
     * @return 结果
     */
    @Override
    public int countVoteResultDetailByOptionId(String optionId) {
        return voteResultDetailMapper.countVoteResultDetailByOptionId(optionId);
    }

    /**
     * 查询业务的投票结果
     *
     * @param request 请求参数
     * @return /
     */
    @Override
    public R<List<GwjGVoteResultInnerDTO>> innerVoteResult(GwjVoteResultInnerRequest request) {
        //查询投票信息
        GwjVoteProject gwjVoteProject = voteProjectService.selectVoteProject(new GwjVoteProject() {{
            setBizNum(request.getBizNum());
            setVoteStatus(GwjVoteStatusEnum.VOTE_ING.getCode());
        }});
        if (StringUtils.isNull(gwjVoteProject)) {
            return R.fail("查无投票项目");
        }
        request.setVoteId(gwjVoteProject.getVoteId());
        //查询投票结果
        List<GwjGVoteResultInnerDTO> results = voteResultDetailMapper.selectVoteResultInnerDTOList(request);
        if (StringUtils.isEmpty(results)) {
            return R.ok(new ArrayList<GwjGVoteResultInnerDTO>());
        }
        return R.ok(results.stream().map(d -> {
            GwjGVoteResultInnerDTO dto = BeanUtil.copyProperties(d, GwjGVoteResultInnerDTO.class);
            for (String s : d.getOwnerPhone().split(",")) {
                dto.setOwnerPhone(s);
            }
            return dto;
        }).filter(CheckDiffList.distinctByKey(GwjGVoteResultInnerDTO::getOwnerPhone)).collect(Collectors.toList()));
    }
}
